package club.dortware.client.module.impl.exploit;

import club.dortware.client.Client;
import club.dortware.client.manager.impl.PropertyManager;
import club.dortware.client.module.annotations.ModuleData;
import club.dortware.client.module.enums.ModuleCategory;
import club.dortware.client.property.impl.StringProperty;
import club.dortware.client.event.impl.UpdateEvent;
import club.dortware.client.module.Module;
import club.dortware.client.util.impl.networking.PacketUtil;
import club.dortware.client.util.impl.time.Stopwatch;
import com.google.common.eventbus.Subscribe;
import com.mojang.authlib.GameProfile;
import net.minecraft.client.network.NetworkPlayerInfo;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.network.play.client.C01PacketChatMessage;
import net.minecraft.network.play.client.C03PacketPlayer;
import net.minecraft.util.ChatAllowedCharacters;

import java.util.Collection;

@ModuleData(name = "PlayerLagger", description = "Lags other players", category = ModuleCategory.EXPLOIT)
public class PlayerLagger extends Module {

    private Stopwatch stopwatch = new Stopwatch();

    @Override
    public void setup() {
        PropertyManager propertyManager = Client.INSTANCE.getPropertyManager();
        propertyManager.add(new StringProperty<>("Mode", this, "Chat", "Paralyze"));
    }

    @Subscribe
    public void onTick(UpdateEvent event) {
        PropertyManager propertyManager = Client.INSTANCE.getPropertyManager();
        String mode = String.valueOf(propertyManager.getProperty(this, "Mode").getValue());
        switch (mode) {
            case "Chat":
                for (NetworkPlayerInfo networkPlayerInfo : (Collection<NetworkPlayerInfo>) mc.getNetHandler().func_175106_d()) {
                    GameProfile gameProfile = networkPlayerInfo.func_178845_a();
                    PacketUtil.sendPacket(new C01PacketChatMessage("/msg " + gameProfile.getName() + " \247ahttps://discord.gg/MyCNYh Albino \2479Ni\2479gger Gang \247a"));
                }
                break;
            case "Paralyze":
                if (isInsidePlayer()) {
                    for (int loop = 0; loop < 500; ++loop) {
                        PacketUtil.sendPacket(new C03PacketPlayer(true));
                    }
                }
                break;
        }
    }

    private boolean isInsidePlayer() {
        for (EntityPlayer entityPlayer : mc.theWorld.playerEntities) {
            if (entityPlayer.getDistanceToEntity(mc.thePlayer) <= 0.995D) {
                return true;
            }
        }
        return false;
    }
    //TODO replace with my own implementation
    public String randomString(int length) {
        StringBuffer sb = new StringBuffer(length);
        while (sb.length() < length) {
            char character = (char)(RANDOM.nextInt() & Character.MAX_VALUE);
            if (Character.isDefined(character)) {
                sb.append(character);
            }
        }
        return ChatAllowedCharacters.filterAllowedCharacters(sb.toString());
    }
}
